<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button type="primary" size="small" @click="showAddRoleDialog(1,'0')">添加权限</el-button>
        </div>
        <!-- row-key表示的是数据中的唯一标识 -->
        <!-- default-expand-all 默认展开子数据 -->
        <el-table border :data="permissionList" row-key="id" default-expand-all>
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template #default="{row}">
              <el-button v-if="row.type==1" size="small" type="text" @click="showAddRoleDialog(2, row.id)">添加权限点</el-button>
              <el-button size="small" type="text" @click="editRole(row.id)">修改</el-button>
              <el-button size="small" type="text" @click="delRold(row.id)">删除</el-button>
              <!-- <el-button size="small" type="text">{{ row }}</el-button> -->
            </template>
          </el-table-column>
        </el-table>
      </el-card>

      <!-- 弹窗 -->
      <!-- 新增编辑的弹层 -->
      <el-dialog :visible="showDialog" :title="showTitle " :close-on-click-modal="false" @close="closeDialog">
        <!-- 表单内容 -->
        <el-form label-width="100px" :model="formData">
          <el-form-item label="权限名称" prop="name">
            <el-input v-model="formData.name" />
          </el-form-item>
          <el-form-item label="权限标识" prop="code">
            <el-input v-model="formData.code" />
          </el-form-item>
          <el-form-item label="权限描述" prop="description">
            <el-input v-model="formData.description" />
          </el-form-item>
          <el-form-item label="权限启用">
            <el-switch
              v-model="formData.enVisible"
              active-color="#13ce66"
              inactive-color="#ff4949"
              active-value="1"
              inactive-value="0"
            />
          </el-form-item>
        </el-form>

        <template #footer>
          <div style="text-align: right;">
            <el-button @click="closeDialog">取消</el-button>
            <el-button type="primary" @click="addPermission">确定</el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getPermissionListApi, addPermissionApi, delPermissionApi, updatePermissionApi, getPermissionDetailApi } from '@/api/permission'
import { transListToTreeData } from '@/utils/index'
export default {
  name: 'Permission',
  data() {
    return {
      permissionList: [],
      showDialog: false,
      // value: false,
      formData: {
        enVisible: '0', // 启用禁用, 0禁用, 1启用
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型标记了一级(页面访问权) 二级(按钮操作权)
        pid: '' // 添加到哪个节点下 当前pid == 父级id
      },
      showTitle: ''
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    async getPermissionList() {
      const { data } = await getPermissionListApi()
      console.log('获取权限列表成功', transListToTreeData(data, '0'))
      this.permissionList = transListToTreeData(data, '0')
    },
    showAddRoleDialog(type, pid) {
      this.showTitle = '新增权限'
      this.showDialog = true
      console.log(type, pid)
      this.formData.type = type
      this.formData.pid = pid
    },
    closeDialog() {
      this.showDialog = false
      // 初始化
      this.formData = {
        enVisible: '0', // 启用禁用, 0禁用, 1启用
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型标记了一级(页面访问权) 二级(按钮操作权)
        pid: '' // 添加到哪个节点下 当前pid == 父级id
      }
    },
    // 添加权限
    async addPermission() {
      if (this.formData.id) {
        // 如果有id是修改
        const res = await updatePermissionApi(this.formData)
        console.log('修改权限成功', res)
        this.$message.success('修改权限成功')
      } else {
        // 否则新增
        const res = await addPermissionApi(this.formData)
        console.log('新增权限成功', res)
        this.$message.success('新增权限成功')
      }
      this.closeDialog()
      this.getPermissionList()
    },
    // 删除权限
    delRold(id) {
      this.$confirm('亲,您确定要删除吗？', '温馨提示').then(async() => {
        const res = await delPermissionApi(id)
        console.log('删除权限成功', res)
        this.$message.success('删除权限成功')
        this.getPermissionList()
      }).catch(() => {
        console.log('取消')
      })
    },
    // 获取修改详情
    async editRole(id) {
      this.showTitle = '修改权限'
      this.showDialog = true
      const { data } = await getPermissionDetailApi(id)
      console.log('获取权限详情成功', data)
      this.formData = data
    }
  }
}
</script>
